package co.yixiang.yshop.module.product.controller.admin.productcoupon;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import co.yixiang.yshop.module.product.controller.admin.productcoupon.vo.*;
import co.yixiang.yshop.module.product.convert.storeproductcouponcdkey.StoreProductCouponCdKeyConvert;
import co.yixiang.yshop.module.product.service.productcoupon.ProductCouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 商品优惠券")
@RestController
@RequestMapping("/product/coupon")
@Validated
public class ProductCouponController {

    @Resource
    private ProductCouponService couponService;


    @PostMapping("/create")
    @Operation(summary = "创建优惠券")
    @PreAuthorize("@ss.hasPermission('product:coupon:create')")
    public CommonResult<Long> createCoupon(@Validated @RequestBody ProductCouponCreateReqVO vo) {
        return success(couponService.createCoupon(vo));
    }

    @PostMapping("/update")
    @Operation(summary = "修改优惠券")
    @PreAuthorize("@ss.hasPermission('product:coupon:update')")
    public CommonResult<Boolean> updateCoupon(@Validated @RequestBody ProductCouponUpdateReqVO vo) {
        couponService.updateCoupon(vo);
        return success(true);
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "优惠券详情")
    @PreAuthorize("@ss.hasPermission('product:coupon:detail')")
    public CommonResult<ProductCouponDetailRespVO> getCouponDetail(@PathVariable Long id) {
        return success(couponService.getCouponDetail(id));
    }

    @GetMapping("/page")
    @Operation(summary = "优惠券分页")
    @PreAuthorize("@ss.hasPermission('product:coupon:query')")
    public CommonResult<PageResult<ProductCouponDetailRespVO>> getStoreProductPage(@Valid StoreProductCouponPageReqVO pageVO) {
        return success(couponService.getCouponPage(pageVO));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除优惠券")
    @PreAuthorize("@ss.hasPermission('product:coupon:delete')")
    public CommonResult<Boolean> deleteCoupon(@PathVariable Long id) {
        return success(couponService.deleteCoupon(id));
    }

    @GetMapping("/canvas/page")
    @Operation(summary = "画布优惠券分页")
    @PreAuthorize("@ss.hasPermission('product:coupon:canvas:page')")
    public CommonResult<PageResult<CanvasProductCouponRespVO>> getCanvasProductPage(@Valid CanvasProductCouponPageReqVO pageVO) {
        return success(couponService.page(pageVO));
    }

    @GetMapping("/record")
    @Operation(summary = "优惠券发放记录")
    @PreAuthorize("@ss.hasPermission('product:coupon:record')")
    public CommonResult<PageResult<ProductCouponIssueRecordRespVO>> couponIssueRecord(ProductCouponIssueRecordReqVO pageVO) {
        return success(couponService.couponIssueRecord(pageVO));
    }

    @DeleteMapping("/delete/cdkey/{ids}")
    @Operation(summary = "删除优惠券兑换码")
    @PreAuthorize("@ss.hasPermission('product:coupon:delete:cdkey')")
    public CommonResult<Boolean> deleteCouponCdKey(@PathVariable Long[] ids) {
        couponService.deleteCouponCdKey(ids);
        return success(true);
    }

    @GetMapping("/cdkey/export-excel")
    @Operation(summary = "导出兑换码")
    @PreAuthorize("@ss.hasPermission('product:coupon:export:cdkey')")
    @OperateLog(type = EXPORT)
    public void export(@Valid ProductCouponIssueRecordReqVO pageVO,
                       HttpServletResponse response) throws IOException {
        List<ProductCouponIssueRecordRespVO> data = couponService.exportRecordCdKey(pageVO);
        List<ProductCouponCdKeyExcelVO> result = StoreProductCouponCdKeyConvert.INSTANCE.convertList(data);
        ExcelUtils.write(response, "优惠券兑换码.xls", "兑换码", ProductCouponCdKeyExcelVO.class, result);
    }
}
